/*
 * Copyright (C) 2010 Zaizi Limited.
 * 
 * This file is part of Zaizi's Alfresco FISE integration
 * 
 * Alfresco FISE is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Alfresco FISE is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Alfresco FISE. If not, see <http://www.gnu.org/licenses/>.
 */
package org.zaizi.fiseiks.alfresco.webscript;

import java.io.IOException;

import org.alfresco.service.ServiceRegistry;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.apache.log4j.Logger;
import org.springframework.extensions.webscripts.AbstractWebScript;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.springframework.extensions.webscripts.WebScriptResponse;
import org.zaizi.fiseiks.alfresco.service.FiseIksSearchService;
import org.zaizi.fiseiks.alfresco.service.SemanticContent;

/**
 * Semantic content retrieval WebScript
 * 
 * @author efoncubierta
 * @see AbstractWebScript
 */
public class GetSemanticContent extends AbstractWebScript
{
	
	private static Logger logger = Logger.getLogger(GetSemanticContent.class);

	@SuppressWarnings("unused")
	private ServiceRegistry services;
	private NodeService nodeService;
	private FiseIksSearchService fiseSearchService;
	
	/**
	 * Inject Alfresco's service registry
	 * 
	 * @param services Registry of Alfresco's service registry
	 */
	public void setServices(ServiceRegistry services)
	{
		this.services = services;
		this.nodeService = services.getNodeService();
	}
	
	/**
	 * Inject FISE search service
	 * 
	 * @param fiseSearchService FISE search service
	 */
	public void setFiseSearchService(FiseIksSearchService fiseSearchService)
	{
		this.fiseSearchService = fiseSearchService;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.springframework.extensions.webscripts.WebScript#execute(org.springframework.extensions.webscripts.WebScriptRequest, org.springframework.extensions.webscripts.WebScriptResponse)
	 */
	public void execute(WebScriptRequest req, WebScriptResponse res)
	{
		// Check for nodeRef parameter
		final String nodeRefStr = req.getParameter("nodeRef");
		if(nodeRefStr == null)
		{
			logger.error("Unspecified parameter nodeRef");
			return;
		}
		NodeRef nodeRef = new NodeRef(nodeRefStr);
		
		// Check permissions and load semantic information
		SemanticContent content = new SemanticContent();
		if(nodeService.exists(nodeRef))
		{
			content = fiseSearchService.get(nodeRef);
		}

		try
		{
			res.setContentType("application/json");
			res.getWriter().append(content.toJSONString());
		}
		catch (IOException e)
		{
			// TODO: Handle error
			logger.error(e.getMessage(), e);
		}
	}
}